﻿using DocumentFormat.OpenXml.Lite.Helper;
using DocumentFormat.OpenXml.Lite.WordprocessingML.Word.Types;
using DocumentFormat.OpenXml.Lite.Xml;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;

namespace DocumentFormat.OpenXml.Lite.WordprocessingML.Word.RunProperties
{
    [XmlRoot("bdr", Namespace = Constants.WordMain)]
    public class Border : XmlSerializable, IRunPropElement
    {
        [XmlAttribute("val", Namespace = Constants.WordMain, Form = XmlSchemaForm.Qualified)]
        public ST_Border Value { get; set; }

        /// <summary>
        /// Hexadecimal number. Length: 3
        /// </summary>
        [XmlAttribute("color", Namespace = Constants.WordMain, Form = XmlSchemaForm.Qualified)]
        public string Color
        {
            get { return color; }
            set
            {
                if (value == null || (value.Length != 3 && value != "auto") ||
                    (value.Length == 3 && !Helpers.ParseHexadecimalValue(value)))
                {
                    throw new ArgumentException("String: " + value +
                        "is not a valid hexadecimal string or length is not 3!");
                }

                color = value;
            }
        }

        [Optional]
        [XmlAttribute("themeColor", Namespace = Constants.WordMain, Form = XmlSchemaForm.Qualified)]
        public Nullable<ST_ThemeColor> ThemeColor { get; set; }

        /// <summary>
        /// Hexadecimal number. Length: 1
        /// </summary>
        [Optional]
        [XmlAttribute("themeTint", Namespace = Constants.WordMain, Form = XmlSchemaForm.Qualified)]
        public string ThemeColorTint
        {
            get { return themeColorTint; }
            set
            {
                if (value == null || value.Length != 1 || !Helpers.ParseHexadecimalValue(value))
                {
                    throw new ArgumentException("String: " + value +
                        "is not a valid hexadecimal string or length is not 1!");
                }

                themeColorTint = value;
            }
        }

        /// <summary>
        /// Hexadecimal number. Length: 1
        /// </summary>
        [Optional]
        [XmlAttribute("themeShade", Namespace = Constants.WordMain, Form = XmlSchemaForm.Qualified)]
        public string ThemeColorShade
        {
            get { return themeColorShade; }
            set
            {
                if (value == null || value.Length != 1 || !Helpers.ParseHexadecimalValue(value))
                {
                    throw new ArgumentException("String: " + value +
                        "is not a valid hexadecimal string or length is not 1!");
                }

                themeColorShade = value;
            }
        }

        [Optional]
        [XmlAttribute("sz", Namespace = Constants.WordMain, Form = XmlSchemaForm.Qualified)]
        public uint Width { get; set; }

        [Optional]
        [XmlAttribute("space", Namespace = Constants.WordMain, Form = XmlSchemaForm.Qualified)]
        public uint Spacing { get; set; }

        [Optional]
        [XmlAttribute("shadow", Namespace = Constants.WordMain, Form = XmlSchemaForm.Qualified)]
        public Nullable<ST_OnOff> Shadow { get; set; }

        [Optional]
        [XmlAttribute("frame", Namespace = Constants.WordMain, Form = XmlSchemaForm.Qualified)]
        public Nullable<ST_OnOff> FrameEffect { get; set; }

        [XmlIgnore]
        private string color;

        [XmlIgnore]
        private string themeColorTint;

        [XmlIgnore]
        private string themeColorShade;

        public Border() { }

        public void Copy(object obj)
        {
            if (obj is Border)
            {
                this.Value = (obj as Border).Value;

                this.Color = (obj as Border).Color;
                this.ThemeColor = (obj as Border).ThemeColor;
                this.ThemeColorTint = (obj as Border).ThemeColorTint;
                this.ThemeColorShade = (obj as Border).ThemeColorShade;

                this.Width = (obj as Border).Width;
                this.Spacing = (obj as Border).Spacing;
                this.Shadow = (obj as Border).Shadow;
                this.FrameEffect = (obj as Border).FrameEffect;
            }
        }
    }
}
